import { createRouter, createWebHistory } from "vue-router";
import { useUser } from "@/stores/user";

import page from "@/views/todolist/page.vue";
import personal from "@/views/todolist/personal.vue";
import todo from "@/views/todolist/todo.vue";
import test from "@/views/todolist/test.vue";
import login from "@/components/login.vue";
import my from "@/views/todolist/my.vue";

import classpage from "@/views/codelover/classpage.vue";
import grades from "@/views/codelover/grades.vue";

import registration from "@/views/codelover/registration.vue";

import movie from "@/views/movie/movie.vue";

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      // todolist
      path: "/page",
      name: "page",
      component: page,
      // component: () => import('@/views/page.vue'),
      meta: {
        title: "个人简介",
        requiresAuth: true,
      },
    },
    {
      path: "/",
      name: "personal",
      component: personal,
      // component: ()=>import('@/views/personal.vue'),
      meta: {
        title: "首页",
        requiresAuth: true,
      },
    },
    {
      path: "/todo",
      name: "todo",
      component: todo,
      // component: ()=>import('@/views/todo.vue'),
      meta: {
        title: "待办事件列表",
        requiresAuth: true,
      },
    },
    {
      path: "/login",
      name: "login",
      component: login,
      // component: ()=>import('@/components/login.vue'),
      meta: { title: "登入" },
    },
    {
      path: "/test",
      name: "test",
      component: test,
      // component: ()=>import('@/views/todolist/test.vue'),
      meta: {
        title: "测试",
        requiresAuth: true,
      },
    },
    {
      path: "/my",
      name: "my",
      component: my,
      // component: () => import("@/views/todolist/my.vue"),
      meta: {
        title: "我的",
        requiresAuth: true,
      },
    },
    {
      path: "/classpage",
      name: "classpage",
      component: classpage,
      // component: ()=>import('@/views/todolist/test.vue'),
      meta: {
        title: "课程成绩",
        requiresAuth: true,
      },
    },
    {
      path: "/grades",
      name: "grades",
      component: grades,
      // component: ()=>import('@/views/todolist/test.vue'),
      meta: {
        title: "个人成绩",
        requiresAuth: true,
      },
    },
    {
      path: "/task/:course",
      component: () => import("../views/codelover/task.vue"),
    },

    {
      path: "/registration",
      name: "registration",
      component: registration,
      // component: ()=>import('@/views/todolist/test.vue'),
      meta: { title: "注册" },
    },

    // {
    //   // 电影
    //   path: "/movie",
    //   name: "movie",
    //   component: movie,
    //   // component: ()=>import('@/views/movie.vue'),
    //   meta: { title: "电影列表" },
    //   children: [
    //     {
    //       path: "/channel",
    //       name: "channel",
    //       component: () => import("@/views/movie/channel.vue"),
    //       meta: { title: "频道" },
    //     },
    //     {
    //       path: "/homepage",
    //       name: "homepage",
    //       component: () => import("@/views/movie/homepage.vue"),
    //       meta: { title: "发现" },
    //     },
    //     {
    //       path: "/video",
    //       name: "video",
    //       component: () => import("@/views/movie/video.vue"),
    //       meta: { title: "播放器" },
    //     },
    //     {
    //       path: "/video/:movie_id",
    //       component: () => import("@/views/movie/video.vue"),
    //     },
    //   ],
    // },
  ],
});

//-前置守卫路由:登录校验
router.beforeEach((to, from, next) => {
  const userStore = useUser();
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    // 验证用户是否登录
    if (!userStore.isLoggedIn) {
      // 用户未登录，重定向到登录页面
      next({
        path: "/login",
        query: { redirect: to.fullPath },
      });
    } else {
      next();
    }
  } else {
    next();
  }
});

// 导出对象
export default router;
